﻿using LightwaveRF;
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace lightwaverf
{
    public static class API
    {
        /// <summary>
        /// index used for requests to the wifilink
        /// </summary>
        static int ind = -999;
        /// <summary>
        /// get the next index and return it.
        /// </summary>
        private static string nextind
        {
            get
            {
                if (ind == -999)//index not yet initialized. - set it to a random number.
                {
                    Random r = new Random();
                    ind = r.Next(999);
                }
                ind++;
                if (ind > 999) ind = 1;
                return (ind.ToString("000"));
            }
        }
        /// <summary>
        /// Regex for on/off
        /// matches :Room, Device, and State
        /// </summary>
        public const string OnOffRegEx = "...,(!R)(?<Room>[0-9])(D)(?<Device>[0-9])(F)(?<State>[0-1])";
        /// <summary>
        /// Regex for Dim
        /// Matches: Room, Device, State
        /// </summary>
        public const string dimRegEx = "...,(!R)(?<Room>[0-9])(D)(?<Device>[0-9])(FdP)(?<State>[0-9][0-9])";//"533,!R" + Room + "D" + Device + "FdP" + pstr + "|"

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Room">room number </param>
        /// <param name="Device">device number</param>
        /// <param name="percent">percentage level for the dim< eg. 50/param>
        /// <returns>String "OK" otherwise error message</returns>
        public static void Dim(int room, int device, int percent)
        {
            string pstr;
            if (percent == 0) percent = 1;
            pstr = Math.Round(((double)percent / 100 * 32)).ToString();
            string text = nextind + ",!R" + room + @"D" + device + @"FdP" + pstr + @"|";
            sendRaw(text);
        }
        /// <summary>
        /// send on/off command to a room/device
        /// </summary>
        /// <param name="Room">room number </param>
        /// <param name="Device">device number</param>
        /// <param name="state">state (0 or 1)</param>
        /// <returns>String "OK" otherwise error message</returns>
        public static void DeviceOnOff(int room, int device, State state)
        {
            string text = nextind + ",!R" + room + @"D" + device + @"F" + StateStrings.GetStateString(state) + @"|";
            sendRaw(text);
        }

        /// <summary>
        /// Send raw packet containing 'text' to the wifilink
        /// </summary>
        /// <param name="text">contents of packet.</param>
        public static void sendRaw(string text)
        {
            var udpClient = new UdpClient();
            IPEndPoint endPoint = new IPEndPoint(IPAddress.Broadcast, 9760);
            byte[] send_buffer = Encoding.ASCII.GetBytes(text);
            udpClient.Send(send_buffer, send_buffer.Length, endPoint);
        }
    }
}